package com.zjh.springsecurity03.config;

import com.zjh.springsecurity03.mapper.UserMapper;
import com.zjh.springsecurity03.pojo.Role;
import com.zjh.springsecurity03.pojo.User;
import lombok.AllArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Set;

@Component
@AllArgsConstructor
public class MyUserDetialsService implements UserDetailsService {
    private final UserMapper userMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userMapper.getUserByName(username);
        if(ObjectUtils.isEmpty( user)) throw new UsernameNotFoundException("用户不存在");
        List<Role> rolesByUid = userMapper.getRolesByUid(user.getId());
        user.setRoles(rolesByUid);
        return user;
    }
}
